<template>
  <div id="code-editor" ref="codeEditorRef" style="min-height: 500px;  height:72vh" ></div>
</template>
  
<style scoped>
</style>

<script setup lang="ts">
import * as monaco from 'monaco-editor';
import { onMounted, ref, toRaw, watch } from 'vue';

interface Props {
  value:string,
  language?:string,
  handleChange:(v:string)=>void;
}
const codeEditorRef=ref();
const codeEditor=ref();

const props = withDefaults(defineProps<Props>(),{
  value:() => "",
  language:() => "java",
  handleChange:(v:string) =>{
    console.log(v);
  },
});

watch(
  ()=>props.language,
  ()=>{
  if(codeEditor.value){
    monaco.editor.setModelLanguage(
      toRaw(codeEditor.value).getModel(),
      props.language
      );
  }
})

const value=ref("hello world");

onMounted(()=>{
    if(!codeEditorRef.value){
    return;
   }
  codeEditor.value=monaco.editor.create(codeEditorRef.value, {
	value:props.value,
	language: props.language,
	automaticLayout: true,
  minimap:{
    enabled:true,
  },
  theme:"vs-dark",
  colorDecorators:true,
  readOnly:false,
  });

  codeEditor.value.onDidChangeModelContent(()=>{
    props.handleChange(toRaw(codeEditor.value).getValue());
  });
});
</script>